package com.isnotlocus.gulimail.ware.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.isnotlocus.gulimail.ware.PurchaseMergeVo;
import com.isnotlocus.gulimail.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.isnotlocus.gulimail.ware.entity.PurchaseEntity;
import com.isnotlocus.gulimail.ware.service.PurchaseService;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;


/**
 * 采购信息
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 20:28:44
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 查询出当前没有派出采购的采购单
     * @param map
     * @return
     */
    //purchase/unreceive/list
    @GetMapping("/unreceive/list")
    public R GetUnreceiveList(@RequestParam Map<String, Object> map) {
        PageUtils page = purchaseService.GetUnreceiveList(map);
        return R.ok().put("page", page);
    }

    //purchase/merge
    @PostMapping("/merge")
    public  R purchaseMerge(@RequestBody PurchaseMergeVo purchaseMergeVo){
        purchaseService.purchaseMerge(purchaseMergeVo);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setUpdateTime(new Date());
        purchase.setCreateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 采购单领取
     * @param Ids  领取的那些采购单
     * @return
     */
    @PostMapping("/received")
    public R received(@RequestBody List<Long> Ids){

        purchaseService.receivedPurchase(Ids);
          return R.ok();
    }

    @PostMapping("/done")
    public R done(@RequestBody PurchaseDoneVo vo) throws IOException {

        purchaseService.DonPurchase(vo);
        return  R.ok();
    }


}
